<?php

class Controller{
    
    /**
     * 
     *
     * @var SP_Route
     */
    var $uri;
    var $input;
    var $loader;
    /**
     * 
     *
     * @var SP_DB
     */
    var $db;
    /**
     *
     * @var SP_Output
     */
    var $output;
    /**
     *
     * @var SP_Auth
     */
    var $auth;

    var $nowCtrl;
    var $nowAct;
    
    function Controller(){
        $this->loader =& Loader::instance();
        //载入配置文件
        $this->config =& Loader::config();

        $this->uri =& Loader::lib('uri');
        $this->output =& Loader::lib('output');
        $this->db =& Loader::database('db');

        $this->nowCtrl = $this->uri->getClass();
        $this->nowAct = $this->uri->getMethod();

        $this->input['get'] = $_GET;

        if($this->isPost()){
            $this->input['post'] = $_POST;
        }
        if($this->config['auth_open']){
            $this->auth =& Loader::lib('auth');
        }
    }
    
    function pre_controller(){
        ;
    }
    function post_controller(){
        ;
    }

    function isPost(){
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
            return true;
        }
        return false;
    }
    
    //URI路由重新分发之后的参数
    function rsegment($n, $no_result = FALSE){
        return $this->uri->rsegment($n,$no_result);
    }
    //获取URI参数
    function segment($n, $no_result = FALSE){
        return $this->uri->segment($n,$no_result);
    }
    
    function getRsegments(){
        return $this->uri->rsegments;
    }
    function getSegments(){
        return $this->uri->segments;
    }

    function getPost($key,$default = ''){
        if(isset($this->input['post'][$key])){
            return $this->input['post'][$key];
        }else{
            return $default;
        }
    }

    function getGet($key,$default = ''){
        if(isset($this->input['get'][$key])){
            return $this->input['get'][$key];
        }else{
            return $default;
        }
    }
    
    function getRequest($key,$default = ''){
        if(isset($this->input['get'][$key])){
            return $this->input['get'][$key];
        }elseif(isset($this->input['post'][$key])){
            return $this->input['post'][$key];
        }else{
            return $default;
        }
    }

    function gets(){
        return $this->input['get'];
    }
    function posts(){
        return $this->input['post'];
    }
    function requests(){
        return array_merge($this->input['get'],$this->input['post']);
    }
}